<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="年份">
          <a-select v-decorator="['year', {
            rules: [
              {
                required: true,
                message: '年份不能为空!',
              },
            ],
            }]" placeholder="请选择年份" @change="handleChange">
            <a-select-option v-for="(i,index) in yearList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-divider>工业</a-divider>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="钢铁">
          <a-input v-decorator="['industrySteel', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入钢铁数量" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="石化化工">
          <a-input v-decorator="['industryPetrochemical', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入石化化工数量" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="其他工业">
          <a-input v-decorator="['industryOther', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入其他工业数量" />
        </a-form-item>
        <a-divider>交通</a-divider>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="航空">
          <a-input v-decorator="['trafficAviation', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入航空数量" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="水运">
          <a-input v-decorator="['trafficWaterTransport', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入水运数量" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="其他交通">
          <a-input v-decorator="['trafficOther', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入其他交通数量" />
        </a-form-item>
        <a-divider>建筑</a-divider>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="居民生活">
          <a-input v-decorator="['buildResidentsLife', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入居民生活数量" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="服务业">
          <a-input v-decorator="['buildService', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入服务业数量" />
        </a-form-item>
        <a-divider>农业</a-divider>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="农业">
          <a-input v-decorator="['farme', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
            }]" placeholder="请输入农业数量" />
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction, postAction } from '@api/manage'

const date = new Date()

export default {
  name: 'fieldCarbonModal',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '50px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      selectList: [
        { name: '能源电力', key: '能源电力' },
        { name: '农业', key: '农业' },
        { name: '工业', key: '工业' },
        { name: '钢铁', key: '钢铁' },
        { name: '石化化工', key: '石化化工' },
        { name: '工业其他', key: '工业其他' },
        { name: '建筑业', key: '建筑业' },
        { name: '交通', key: '交通' },
      ],
      yearList: [
        { name: date.getFullYear() - 1, key: date.getFullYear() - 1 },
        { name: date.getFullYear() - 2, key: date.getFullYear() - 2 },
        { name: date.getFullYear() - 3, key: date.getFullYear() - 3 },
        { name: date.getFullYear() - 4, key: date.getFullYear() - 4 },
        { name: date.getFullYear() - 5, key: date.getFullYear() - 5 },
        { name: date.getFullYear() - 6, key: date.getFullYear() - 6 },
        { name: date.getFullYear() - 7, key: date.getFullYear() - 7 },
        { name: date.getFullYear() - 8, key: date.getFullYear() - 8 },
      ],
      url: {
        add: '/carbon/energyAnalysisTerritoryType/add',
        edit: '/carbon/energyAnalysisTerritoryType/edit'
      }
    }
  },methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'year',
          'industrySteel',
          'industryPetrochemical',
          'industryOther',
          'trafficAviation',
          'trafficWaterTransport',
          'trafficOther',
          'buildResidentsLife',
          'buildService',
          'farme',
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          httpAction(httpurl, formData, method).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('refresh')
            } else {
              that.$message.error(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
            that.$parent.$parent.loadData()
          })
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>

</style>